In [1]:
# INSTALL REQUIRED LIBRARIES
!pip install yfinance
!pip install beautifulsoup4
!pip install lxml
!pip install plotly
Requirement already satisfied: yfinance in c:\users\sanskriti\anaconda3\lib\site-packages (0.2.65) Requirement already satisfied: pandas>=1.3.0 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (2.2.2) Requirement already satisfied: numpy>=1.16.5 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (1.26.4) Requirement already satisfied: requests>=2.31 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (2.32.2) Requirement already satisfied: multitasking>=0.0.7 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (0.0.11) Requirement already satisfied: platformdirs>=2.0.0 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (3.10.0) Requirement already satisfied: pytz>=2022.5 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (2024.1) Requirement already satisfied: frozendict>=2.3.4 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (2.4.2) Requirement already satisfied: peewee>=3.16.2 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (3.18.2) Requirement already satisfied: beautifulsoup4>=4.11.1 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (4.12.3) Requirement already satisfied: curl_cffi>=0.7 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (0.12.0) Requirement already satisfied: protobuf>=3.19.0 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (3.20.3) Requirement already satisfied: websockets>=13.0 in c:\users\sanskriti\anaconda3\lib\site-packages (from yfinance) (15.0.1) Requirement already satisfied: soupsieve>1.2 in c:\users\sanskriti\anaconda3\lib\site-packages (from beautifulsoup4>=4.11.1->yfinance) (2.5) Requirement already satisfied: cffi>=1.12.0 in c:\users\sanskriti\anaconda3\lib\site-packages (from curl_cffi>=0.7->yfinance) (1.16.0) Requirement already satisfied: certifi>=2024.2.2 in c:\users\sanskriti\anaconda3\lib\site-packages (from curl_cffi>=0.7->yfinance) (2024.8.30) Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\sanskriti\anaconda3\lib\site-packages (from pandas>=1.3.0->yfinance) (2.9.0.post0) Requirement already satisfied: tzdata>=2022.7 in c:\users\sanskriti\anaconda3\lib\site-packages (from pandas>=1.3.0->yfinance) (2023.3) Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\sanskriti\anaconda3\lib\site-packages (from requests>=2.31->yfinance) (2.0.4) Requirement already satisfied: idna<4,>=2.5 in c:\users\sanskriti\anaconda3\lib\site-packages (from requests>=2.31->yfinance) (3.7) Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\sanskriti\anaconda3\lib\site-packages (from requests>=2.31->yfinance) (2.2.2) Requirement already satisfied: pycparser in c:\users\sanskriti\anaconda3\lib\site-packages (from cffi>=1.12.0->curl_cffi>=0.7->yfinance) (2.21) Requirement already satisfied: six>=1.5 in c:\users\sanskriti\anaconda3\lib\site-packages (from python-dateutil>=2.8.2->pandas>=1.3.0->yfinance) (1.16.0)
[notice] A new release of pip is available: 24.3.1 -> 25.1.1 [notice] To update, run: python.exe -m pip install --upgrade pip
Requirement already satisfied: beautifulsoup4 in c:\users\sanskriti\anaconda3\lib\site-packages (4.12.3) Requirement already satisfied: soupsieve>1.2 in c:\users\sanskriti\anaconda3\lib\site-packages (from beautifulsoup4) (2.5)
[notice] A new release of pip is available: 24.3.1 -> 25.1.1 [notice] To update, run: python.exe -m pip install --upgrade pip
Requirement already satisfied: lxml in c:\users\sanskriti\anaconda3\lib\site-packages (5.2.1)
[notice] A new release of pip is available: 24.3.1 -> 25.1.1 [notice] To update, run: python.exe -m pip install --upgrade pip
Requirement already satisfied: plotly in c:\users\sanskriti\anaconda3\lib\site-packages (5.22.0) Requirement already satisfied: tenacity>=6.2.0 in c:\users\sanskriti\anaconda3\lib\site-packages (from plotly) (8.2.2) Requirement already satisfied: packaging in c:\users\sanskriti\anaconda3\lib\site-packages (from plotly) (23.2)
[notice] A new release of pip is available: 24.3.1 -> 25.1.1 [notice] To update, run: python.exe -m pip install --upgrade pip
In [3]:
# IMPORTS
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
In [5]:
tesla = yf.Ticker("TSLA")
tesla_data = tesla.history(period="max")
tesla_data.reset_index(inplace=True)
tesla_data.head()
Out[5]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2010-06-29 00:00:00-04:00 | 1.266667 | 1.666667 | 1.169333 | 1.592667 | 281494500 | 0.0 | 0.0 |
| 1 | 2010-06-30 00:00:00-04:00 | 1.719333 | 2.028000 | 1.553333 | 1.588667 | 257806500 | 0.0 | 0.0 |
| 2 | 2010-07-01 00:00:00-04:00 | 1.666667 | 1.728000 | 1.351333 | 1.464000 | 123282000 | 0.0 | 0.0 |
| 3 | 2010-07-02 00:00:00-04:00 | 1.533333 | 1.540000 | 1.247333 | 1.280000 | 77097000 | 0.0 | 0.0 |
| 4 | 2010-07-06 00:00:00-04:00 | 1.333333 | 1.333333 | 1.055333 | 1.074000 | 103003500 | 0.0 | 0.0 |
In [7]:
url = "https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue"
headers = {"User-Agent": "Mozilla/5.0"}
html_data = requests.get(url, headers=headers).text
soup = BeautifulSoup(html_data, "html.parser")
tables = soup.find_all("table")
for table in tables:
if "Tesla Quarterly Revenue" in str(table):
tesla_revenue = pd.read_html(str(table))[0]
break
tesla_revenue.columns = ["Date", "Revenue"]
tesla_revenue["Revenue"] = tesla_revenue["Revenue"].replace(r"[\$,]", "", regex=True)
tesla_revenue.dropna(inplace=True)
tesla_revenue.tail()
C:\Users\Sanskriti\AppData\Local\Temp\ipykernel_2288\2438719197.py:9: FutureWarning: Passing literal html to 'read_html' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object. tesla_revenue = pd.read_html(str(table))[0]
Out[7]:
| Date | Revenue | |
|---|---|---|
| 58 | 2010-09-30 | 31 |
| 59 | 2010-06-30 | 28 |
| 60 | 2010-03-31 | 21 |
| 62 | 2009-09-30 | 46 |
| 63 | 2009-06-30 | 27 |
In [9]:
gamestop = yf.Ticker("GME")
gme_data = gamestop.history(period="max")
gme_data.reset_index(inplace=True)
gme_data.head()
Out[9]:
| Date | Open | High | Low | Close | Volume | Dividends | Stock Splits | |
|---|---|---|---|---|---|---|---|---|
| 0 | 2002-02-13 00:00:00-05:00 | 1.620129 | 1.693350 | 1.603296 | 1.691667 | 76216000 | 0.0 | 0.0 |
| 1 | 2002-02-14 00:00:00-05:00 | 1.712707 | 1.716074 | 1.670626 | 1.683250 | 11021600 | 0.0 | 0.0 |
| 2 | 2002-02-15 00:00:00-05:00 | 1.683250 | 1.687458 | 1.658001 | 1.674834 | 8389600 | 0.0 | 0.0 |
| 3 | 2002-02-19 00:00:00-05:00 | 1.666418 | 1.666418 | 1.578048 | 1.607505 | 7410400 | 0.0 | 0.0 |
| 4 | 2002-02-20 00:00:00-05:00 | 1.615920 | 1.662210 | 1.603296 | 1.662210 | 6892800 | 0.0 | 0.0 |
In [11]:
url = "https://www.macrotrends.net/stocks/charts/GME/gamestop/revenue"
headers = {"User-Agent": "Mozilla/5.0"}
html_data = requests.get(url, headers=headers).text
soup = BeautifulSoup(html_data, "html.parser")
tables = soup.find_all("table")
for table in tables:
if "GameStop Quarterly Revenue" in str(table):
gme_revenue = pd.read_html(str(table))[0]
break
gme_revenue.columns = ["Date", "Revenue"]
gme_revenue["Revenue"] = gme_revenue["Revenue"].replace(r"[\$,]", "", regex=True)
gme_revenue.dropna(inplace=True)
gme_revenue.tail()
C:\Users\Sanskriti\AppData\Local\Temp\ipykernel_2288\2552504574.py:9: FutureWarning: Passing literal html to 'read_html' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object. gme_revenue = pd.read_html(str(table))[0]
Out[11]:
| Date | Revenue | |
|---|---|---|
| 61 | 2010-01-31 | 3524 |
| 62 | 2009-10-31 | 1835 |
| 63 | 2009-07-31 | 1739 |
| 64 | 2009-04-30 | 1981 |
| 65 | 2009-01-31 | 3492 |
In [13]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=tesla_data['Date'], y=tesla_data['Close'], name='Stock Price'))
fig.update_layout(title='Tesla Stock Price Over Time', xaxis_title='Date', yaxis_title='Price (USD)')
fig.show()
In [17]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=gme_data['Date'], y=gme_data['Close'], name='Stock Price'))
fig.update_layout(title='GameStop Stock Price Over Time', xaxis_title='Date', yaxis_title='Price (USD)')
fig.show()
In [19]:
fig = go.Figure()
fig.add_trace(go.Scatter(x=tesla_data['Date'], y=tesla_data['Close'], name='Stock Price'))
fig.add_trace(go.Scatter(x=tesla_revenue['Date'], y=tesla_revenue['Revenue'].astype(float), name='Revenue'))
fig.update_layout(title='Tesla: Stock Price vs Revenue', xaxis_title='Date', yaxis_title='USD')
fig.show()
In [ ]: